package cn.darkhorse.mesadmin.controller.device;


import cn.darkhorse.device.pojo.IotGateway;
import cn.darkhorse.device.pojo.MesDevice;
import cn.darkhorse.device.pojo.MesThirdPartyData;
import cn.darkhorse.device.service.IotGatewayService;
import cn.darkhorse.device.service.MesThirdPartyDataService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author maoguanjun
 * @since 2023-11-09
 */
@RestController
@RequestMapping("/device/mesThirdPartyData")
public class MesThirdPartyDataController extends BaseController {
    @Resource
    private MesThirdPartyDataService mesThirdPartyDataService;
    @Resource
    private IotGatewayService iotGatewayService;


    @RequestMapping("/unbound")
    public TableDataInfo unboundList() {
        startPage();
        LambdaQueryWrapper<MesThirdPartyData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(MesThirdPartyData::getGatewayDeviceId);
        List<MesThirdPartyData> list = mesThirdPartyDataService.list(queryWrapper);
        return getDataTable(list);
    }




    @GetMapping("/selectList")
    public TableDataInfo selectList() {
        startPage();
        List<MesThirdPartyData> list = mesThirdPartyDataService.list();
        return getDataTable(list);
    }


    @PostMapping("/add")
    public AjaxResult add(MesThirdPartyData mesThirdPartyData) {
        AjaxResult result = mesThirdPartyDataService.add(mesThirdPartyData);
        return result;
    }

    @PostMapping("/delete")
    public AjaxResult delete(Long id) {
        MesThirdPartyData mesThirdPartyData = mesThirdPartyDataService.getById(id);
        //校验是否关联了设备属性表
        LambdaQueryWrapper<IotGateway> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IotGateway::getGatewayDeviceId, mesThirdPartyData.getGatewayDeviceId());
        IotGateway iotGateway = iotGatewayService.getOne(queryWrapper);
        if (Objects.nonNull(iotGateway)) {
            return AjaxResult.error("已关联了设备属性表,无法删除");
        }
        mesThirdPartyDataService.removeById(id);
        return AjaxResult.success();

    }


}

